package com.ktgj.flight.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.ktgj.common.core.constant.FlightOrderConstants;
import com.ktgj.common.core.utils.helper.StringHelper;
import com.ktgj.flight.common.pay._HuiFu;
import com.ktgj.flight.common.pay._Yeepay;
import com.ktgj.flight.domain.FlightOrderInfo;
import com.ktgj.flight.domain.FlightOrderInfoTickets;
import com.ktgj.flight.domain.InterfaceEntity.Guestlist;
import com.ktgj.flight.domain.InterfaceEntity.MoAutoPayOutTickeResponse;
import com.ktgj.flight.domain.InterfaceEntity.MoAutoPayOutTicketRequest;
import com.ktgj.flight.domain.InterfaceEntity.OutTicketCheckResponse;
import com.ktgj.flight.service.IFlightOrderInfoService;
import com.ktgj.flight.service.IFlightOrderInfoTicketsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TicketConsumerService {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private _Yeepay yeePay;

    @Autowired
    private _HuiFu huiFu;

    @Autowired
    private IFlightOrderInfoService orderInfoService; //订单主表

    @Autowired
    private IFlightOrderInfoTicketsService orderInfoTicketsService; //订单从表


    // 使用JmsListener配置消费者监听的队列，其中name是接收到的消息
    @JmsListener(destination = "YeePayTicketQueue")
    @SendTo("YeePayTicketQueueReturn")
    public String YeePayHandleMessage(String info) {
        if (!Strings.isNullOrEmpty(info))
        {
            OutTicketCheckResponse response = JSON.parseObject(info, OutTicketCheckResponse.class);
            if (response.getTicketSourceWay() == Integer.parseInt(FlightOrderConstants.yeePay.getValue()))
            {
                MoAutoPayOutTickeResponse ticketResponse = yeePay._AutoPayOutTicket(response.getSource(), response.getOrderId(), response.getBigPnr());
                //回填票号信息
                FlightOrderInfo orderInfo = orderInfoService.selectFlightOrderInfo(ticketResponse.OrderID);
                Long orderId = orderInfo.getOrderId();//获取ID
                //获取订单详细信息
                List<FlightOrderInfoTickets> ticketList = orderInfoTicketsService.selectOrderInfoTicketByOrderId(orderId);

                if (ticketResponse.IsSuccess)
                { //成功
                    orderInfo.setPnrstatus(ticketResponse.statusMsg);
                    orderInfoService.updateFlightOrderInfo(orderInfo);

                    for (FlightOrderInfoTickets ticket : ticketList)
                    {
                        String guestName = ticket.getGuestName();
                        String guestIdno = ticket.getGuestIdno();
                        for (Guestlist guestlist : ticketResponse.guestList)
                        {
                            if (guestName.equals(guestlist.guestName) && guestIdno.equals(guestlist.certificateNo))
                            {
                                ticket.setTicketNo(guestlist.ticketNo);
                                orderInfoTicketsService.updateFlightOrderInfoTickets(ticket); //票号
                            }
                        }
                    }
                } else
                {
                    //失败
                    orderInfo.setPnrstatus(ticketResponse.statusMsg);
                    orderInfoService.updateFlightOrderInfo(orderInfo);
                }
            }
        }
        return "消费者接收到信息" + info;
    }

    @JmsListener(destination = "HuifuTicketQueue")
    @SendTo("HuifuTicketQueueReturn")
    public String HuifuHandleMessage(String info)
    {
        if (!StringHelper.isNullOrEmpty(info))
        {
            MoAutoPayOutTicketRequest request = JSON.parseObject(info, MoAutoPayOutTicketRequest.class);
            MoAutoPayOutTickeResponse ticketResponse = huiFu._AutoPayOutTicket(request);
            FlightOrderInfo orderInfo = orderInfoService.selectFlightOrderInfo(ticketResponse.OrderID);
            if (ticketResponse.IsSuccess)
            {
                orderInfo.setPnrstatus(ticketResponse.statusMsg);
            } else
            {
                orderInfo.setPnrstatus(ticketResponse.ErrorMsg);
            }
        }
        return "消费者接收到信息" + info;
    }
}
